package basic.study.wantOffer.chapter3;



/**
 * @author Kevin
 * @date 2020/5/12 23:50
 * 序列化二叉树
 */
public class Problem37 {
    public void Serialize(BinaryTreeNode node, StringBuffer buffer) {
        if (node == null) {
            buffer.append("$");
            return;
        }
        buffer.append(node.value);
        Serialize(node.left, buffer);
        Serialize(node.right, buffer);
    }

    public void Deserialize(BinaryTreeNode node, StringBuffer buffer) {
        StringBuffer temp = buffer.deleteCharAt(0);
        if (temp != null && temp != new StringBuffer("$")) {
            node = new BinaryTreeNode(temp.toString().charAt(0));
            Deserialize(node.left, buffer);
            Deserialize(node.right, buffer);
        }
    }
}

class BinaryTreeNode {
    char value;
    BinaryTreeNode left;
    BinaryTreeNode right;

    public BinaryTreeNode(char value) {
        this.value = value;
    }
}
